import { Box3 } from 'three'
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js'
import { GLTF, GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'

export class ModelUtil {
  /** 加载gltf模型 */
  static loadGltf(url: string): Promise<GLTF> {
    const dracoLoader = new DRACOLoader()
    dracoLoader.setDecoderPath('./draco/gltf/')
    return new Promise((resolve, reject) => {
      try {
        const loader = new GLTFLoader()
        loader.setDRACOLoader(dracoLoader)
        loader.load(url, (gltf) => {
          resolve(gltf)
        })
      } catch (error) {
        console.error(error)
        reject(error)
      }
    })
  }

  /** 获取模型的宽、高、深度 */
  static getBoundingBox(gltf: GLTF) {
    // 假设 gltf.scene 是你的GLTF模型的场景
    const box = new Box3().setFromObject(gltf.scene)

    // box.min 和 box.max 现在是包围模型的最小和最大坐标
    const minX = box.min.x
    const minY = box.min.y
    const minZ = box.min.z

    const maxX = box.max.x
    const maxY = box.max.y
    const maxZ = box.max.z

    // 计算包围盒的尺寸
    const width = maxX - minX
    const height = maxY - minY
    const depth = maxZ - minZ

    return { width, height, depth }
  }
}
